import { TransactionManager } from "./TransactionManager"
import { ConnectionInfo } from "./ConnectionInfo"

export class CurdTemplate {
  constructor(private transactionManager: TransactionManager) {
  }

  async getConnectionInfo(): Promise<ConnectionInfo> {
    return await this.transactionManager.getConnectionInfo()
  }

  async query(sql: string, values: any | any[] | { [param: string]: any } = null): Promise<any> {
    const { connection, inTransactional } = await this.getConnectionInfo()
    const res = connection.query(sql, values)
    if (!inTransactional) {
      connection.release()
    }
    return res
  }

  async execute(sql: string, values: any | any[] | { [param: string]: any } = null): Promise<any> {
    const { connection, inTransactional } = await this.getConnectionInfo()
    const res = connection.execute(sql, values)
    if (!inTransactional) {
      connection.release()
    }
    return res
  }

}
